﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace DotNet.Helper
{
    /// <summary>
    /// 加密帮助类
    /// </summary>
    public class SecretHelper
    {
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="input">需要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string Md5(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                throw new ArgumentOutOfRangeException(nameof(input), "input参数不能为空");
            }

            string output;
            byte[] byteArr = Encoding.UTF8.GetBytes(input);
            using (MD5 md5 = new MD5CryptoServiceProvider())
            {
                byte[] result = md5.ComputeHash(byteArr);
                output = BitConverter.ToString(result);
                output = output.Replace("-", "");
            }
            return output;
        }

        /// <summary>
        /// 16位MD5加密
        /// </summary>
        /// <param name="input">需要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string Md5_16(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                throw new ArgumentOutOfRangeException(nameof(input), "input参数不能为空");
            }

            string output;
            byte[] byteArr = Encoding.UTF8.GetBytes(input);
            using (MD5 md5 = new MD5CryptoServiceProvider())
            {
                byte[] result = md5.ComputeHash(byteArr);
                output = BitConverter.ToString(result, 4, 8);
                output = output.Replace("-", "");
            }
            return output;
        }

    }
}
